package cc.rengu.igas.channel.cups.core.service.trans;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.EnchashmentRevsalRequest;
import cc.rengu.igas.channel.cups.facade.response.EnchashmentRevsalResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;

/**
 * 取现冲正
 *
 * @author who
 */
public class CupsEnchashmentRevsalService extends BasePayService<EnchashmentRevsalRequest, EnchashmentRevsalResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean, EnchashmentRevsalRequest enchashmentRevsalRequest,
                                             EnchashmentRevsalResponse enchashmentRevsalResponse) throws Exception {
        //  请求
        CupsField cupsField = cupsFieldBean.getCupsField();
        BeanUtil.beanCopy(enchashmentRevsalRequest, cupsField);
        String tmpAmt = AmountUtil.changeY2F(enchashmentRevsalRequest.getOrderAmt());
        cupsField.setOrderAmt(String.format("%012d", Integer.valueOf(tmpAmt)));
        cupsField.setMsgType("0420");

        //  终端读取能力
        //  IC卡条件代码
        //  终端类型
        cupsFieldBean.createF60()
                .setF60_2_2(enchashmentRevsalRequest.getTermRead())
                .setF60_2_3(enchashmentRevsalRequest.getIcConCode())
                .setF60_2_5(enchashmentRevsalRequest.getTermType());
        cupsFieldBean.build();

        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean, enchashmentRevsalRequest.getHeader().getTxnNum());
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, enchashmentRevsalResponse);
        enchashmentRevsalResponse.setRespCode(cupsFieldRsp.getRespCode());
        if (CupsParamConstant.SUCCESS_CODE.equals(enchashmentRevsalResponse.getRespCode())) {
            enchashmentRevsalResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            enchashmentRevsalResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        return new ChnlBizResponse(enchashmentRevsalResponse);
    }

}
